<?php

declare(strict_types=1);

namespace App\Constants;

/**
 * Class RedisKey
 * @package App\Constants
 * 全局redis缓存 键名
 */
class RedisKey
{
    const EXPIRE_3S = 2;
    const EXPIRE_1MIN = 60;
    const EXPIRE_5MIN = 300;
    const EXPIRE_30MIN = 1800;
    const EXPIRE_1H = 3600;
    const EXPIRE_2H = 7200;
    const EXPIRE_4H = 14400;
    const EXPIRE_24H = 86400;
    const EXPIRE_48H = 172800;
    const EXPIRE_7D = 604800;
    const EXPIRE_60D = 5184000;
    const NO_REPEATED_REQUEST_LOCK = 'request:unrepeated:uid:%s:method:%s:uri:%s';//禁止同一用户重复请求同一接口的锁

    /**
     * USER 用户
     */
    const USER_INFO = 'mc:default:m:user:uid:%u';//用户基本信息
    const USER_GOODS_SMART_SORT = 'smart:store:%s:%u';//用户商品列表简易排序
    const USER_SMART_POPUP = 'smart:popup:%s:%u'; //用户关口弹窗
    const SMART_POPUP_CHANNEL = 'popup_queue_%s_%d'; //关口弹窗通道
    const USER_GAME_CHARACTER = 'user:character:%s:%u';//用户游戏特征数据
    const ALREADY_PULL_CHARACTER = 'already:pull:character:%d:%s:%u';//当天是否拉取过用户游戏特征数据
    const COOKING_USER_GAME_CHARACTER = 'user:character:%s:%s';//cooking用户游戏特征数据

    /**
     * Notes: 获取键名
     * User: hufeng
     * DateTime: 2021/7/8 9:19
     * @param $key
     * @param ...$args
     * @return string
     */
    static public function getKey($key, ...$args)
    {
        return sprintf($key, ...$args);
    }

    /**
     * Notes: 从redis中获取两个有序集合的差集
     * User: 陈朋
     * DateTime: 2021/7/19 18:23
     * @param string $db redis数据库index
     * @param string $key1 有序集合key1
     * @param string $key2 有序集合key2
     * @param string $tmp_key 临时key
     * @param false $withScores 是否返回分数
     * @return array
     */
    public static function zDiff(string $db, string $key1, string $key2, string $tmp_key, bool $withScores = false): array
    {
        $redis = redis($db);
        $redis->zUnionStore($tmp_key, [$key1, $key2], [1, 0], 'MIN');
        $redis->zRemRangeByScore($tmp_key, '0', '0');
        $res = $redis->zRevRange($tmp_key, 0, -1, $withScores);
        $redis->del($tmp_key);
        return $res;
    }
}
